﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GMS.Enums;
using GMS.BasicControls;
using GMS.Dinasty.DAL;
using GMS.Dinasty.BO;
using GMS.MainClasses;


namespace GMS.BasicControls
{
    public partial class InsertCustomer : BackGroundControl
    {
        public InsertCustomer(EditMode mode)
        {
            InitializeComponent();
            _mode = mode;
            if (mode == EditMode.AddIn)
                infoLabel.Text = "Добавление заказчика";
            if (mode == EditMode.Editing)
                infoLabel.Text = "Редактирование заказчика";
            if (mode == EditMode.Deleting)
            {
                infoLabel.Text = "Удаление заказчика";
                infoLabel.ForeColor = Color.Red;
            }

        }
        private EditMode _mode;
        public EditMode Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }
        private int _idUser;
        public int IdUser
        {
            get { return _idUser; }
            set { _idUser = value; }
        }
        public string User
        {
            get { return user.Text; }
            set { user.Text = value; }
        }
        public string Info
        {
            get { return  note.Text; }
            set { note.Text = value; }
        }
        private int _idParentUser;
        public int IdParentUser
        {
            get { return _idParentUser; }
            set { _idParentUser = value; }
        }
        private TreeNode _dataNode;
        public TreeNode DataNode
        {
            get 
            {
                _dataNode = new TreeNode();
                _dataNode.Tag = _idUser;
                _dataNode.Text = user.Text.Replace("/n", string.Empty).Trim();
                _dataNode.ToolTipText = note.Text.Replace("/n", string.Empty).Trim();
                return _dataNode;
            }
            set 
            { 
                _dataNode = value;
                _idUser = Convert.ToInt32(_dataNode.Tag);
                user.Text = _dataNode.Text.Trim();
                note.Text = _dataNode.ToolTipText.Trim();
                if (_dataNode.Parent != null)
                    _idParentUser = Convert.ToInt32(_dataNode.Parent.Tag);
                else
                    _idParentUser = 0;

            }
        }
        private Person _person;
        public Person Person
        {
            get 
            {
                _person = new Person(_idUser);
                _person.Name = user.Text.Replace("/n",string.Empty).Trim();
                _person.Note = note.Text.Replace("/n", string.Empty).Trim();
                _person.ParentId = _idParentUser;
                    return _person; 
            }
            set
            { 
                _person = value;
                _idUser = _person.IdContact;
                user.Text = _person.Name;
                note.Text = _person.Note;
                _idParentUser = _person.ParentId;
            }
        }
        private void user_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter && user.Text != string.Empty)
                userTab.SelectedTab = notePage;
        }

        private void InsertUser_Load(object sender, EventArgs e)
        {
            user.Focus();
        }

        private void note_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                DataAccessCustomer DAC = new DataAccessCustomer();
                switch (this.Mode)
                {
                        
                    case EditMode.AddIn:
                        this._idUser = DAC.InsertCustomer((this.ParentForm as ChildForms).StoredProcedures[10], Person, (this.ParentForm as ChildForms).ConnectionString);
                        if (this._idUser != null)
                        {
                            if (this._idParentUser == 0)
                                (this.Parent as TreeView).Nodes.Add(this.DataNode);
                            else
                            {
                                (this.Parent as TreeView).SelectedNode.Nodes.Add(this.DataNode);
                                (this.Parent as TreeView).SelectedNode.Expand();
                            }
                        }
                        break;
                    case EditMode.Editing:
                        if (DAC.UpdateCustomer((this.ParentForm as ChildForms).StoredProcedures[8], Person, (this.ParentForm as ChildForms).ConnectionString))
                        {
                            (this.Parent as TreeView).SelectedNode.Text = user.Text.Replace("/n", string.Empty).Trim();
                            (this.Parent as TreeView).SelectedNode.ToolTipText = note.Text.Replace("/n", string.Empty).Trim();
                        }
                        break;
                    case EditMode.Deleting:
                        if ((this.Parent as TreeView).SelectedNode.Nodes.Count != 0)
                            MessageBox.Show("Удаление невозможно! Удалите контрагентов!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        else
                        {
                            if (DAC.DeleteCustomer((this.ParentForm as ChildForms).StoredProcedures[11], Person, (this.ParentForm as ChildForms).ConnectionString))
                                (this.Parent as TreeView).SelectedNode.Remove();
                        }
                        break;
                }
                this.Parent.Controls.Remove(this);
            }      
        }

    }
}
